/*   1:    */ package gov.sat.tax.fwsk.util;
/*   2:    */ 
/*   3:    */ public class IDEA4Java
/*   4:    */ {
/*   5:    */   private byte[] Encrypt(byte[] bytekey, byte[] inputBytes, boolean flag)
/*   6:    */   {
/*   7:  5 */     byte[] encryptCode = new byte[8];
/*   8:    */     
/*   9:  7 */     int[] key = get_subkey(flag, bytekey);
/*  10:    */     
/*  11:    */ 
/*  12: 10 */     encrypt(key, inputBytes, encryptCode);
/*  13:    */     
/*  14: 12 */     return encryptCode;
/*  15:    */   }
/*  16:    */   
/*  17:    */   private int bytesToInt(byte[] inBytes, int startPos)
/*  18:    */   {
/*  19: 16 */     return (inBytes[startPos] << 8 & 0xFF00) + (
/*  20: 17 */       inBytes[(startPos + 1)] & 0xFF);
/*  21:    */   }
/*  22:    */   
/*  23:    */   private void intToBytes(int inputInt, byte[] outBytes, int startPos)
/*  24:    */   {
/*  25: 21 */     outBytes[startPos] = ((byte)(inputInt >>> 8));
/*  26: 22 */     outBytes[(startPos + 1)] = ((byte)inputInt);
/*  27:    */   }
/*  28:    */   
/*  29:    */   private int x_multiply_y(int x, int y)
/*  30:    */   {
/*  31: 26 */     if (x == 0)
/*  32:    */     {
/*  33: 27 */       x = 65537 - y;
/*  34:    */     }
/*  35: 28 */     else if (y == 0)
/*  36:    */     {
/*  37: 29 */       x = 65537 - x;
/*  38:    */     }
/*  39:    */     else
/*  40:    */     {
/*  41: 31 */       int tmp = x * y;
/*  42: 32 */       y = tmp & 0xFFFF;
/*  43: 33 */       x = tmp >>> 16;
/*  44: 34 */       x = y - x + (y < x ? 1 : 0);
/*  45:    */     }
/*  46: 36 */     return x & 0xFFFF;
/*  47:    */   }
/*  48:    */   
/*  49:    */   private void encrypt(int[] key, byte[] inbytes, byte[] outbytes)
/*  50:    */   {
/*  51: 40 */     int k = 0;
/*  52: 41 */     int a = bytesToInt(inbytes, 0);
/*  53: 42 */     a = changeBit(a);
/*  54: 43 */     int b = bytesToInt(inbytes, 2);
/*  55: 44 */     b = changeBit(b);
/*  56: 45 */     int c = bytesToInt(inbytes, 4);
/*  57: 46 */     c = changeBit(c);
/*  58: 47 */     int d = bytesToInt(inbytes, 6);
/*  59: 48 */     d = changeBit(d);
/*  60: 49 */     for (int i = 0; i < 8; i++)
/*  61:    */     {
/*  62: 50 */       a = x_multiply_y(a, key[(k++)]);
/*  63: 51 */       b += key[(k++)];
/*  64: 52 */       b &= 0xFFFF;
/*  65: 53 */       c += key[(k++)];
/*  66: 54 */       c &= 0xFFFF;
/*  67: 55 */       d = x_multiply_y(d, key[(k++)]);
/*  68: 56 */       int tmp1 = b;
/*  69: 57 */       int tmp2 = c;
/*  70: 58 */       c ^= a;
/*  71: 59 */       b ^= d;
/*  72: 60 */       c = x_multiply_y(c, key[(k++)]);
/*  73: 61 */       b += c;
/*  74: 62 */       b &= 0xFFFF;
/*  75: 63 */       b = x_multiply_y(b, key[(k++)]);
/*  76: 64 */       c += b;
/*  77: 65 */       c &= 0xFFFF;
/*  78: 66 */       a ^= b;
/*  79: 67 */       d ^= c;
/*  80: 68 */       b ^= tmp2;
/*  81: 69 */       c ^= tmp1;
/*  82:    */     }
/*  83: 71 */     intToBytes(x_multiply_y(a, key[(k++)]), outbytes, 0);
/*  84: 72 */     intToBytes(c + key[(k++)], outbytes, 2);
/*  85: 73 */     intToBytes(b + key[(k++)], outbytes, 4);
/*  86: 74 */     intToBytes(x_multiply_y(d, key[k]), outbytes, 6);
/*  87:    */   }
/*  88:    */   
/*  89:    */   private int[] encrypt_subkey(byte[] byteKey)
/*  90:    */   {
/*  91: 78 */     int[] key = new int[52];
/*  92: 79 */     if (byteKey.length < 16)
/*  93:    */     {
/*  94: 80 */       byte[] tmpkey = new byte[16];
/*  95: 81 */       System.arraycopy(byteKey, 0, tmpkey, 
/*  96: 82 */         tmpkey.length - byteKey.length, byteKey.length);
/*  97: 83 */       byteKey = tmpkey;
/*  98:    */     }
/*  99: 85 */     for (int i = 0; i < 8; i++)
/* 100:    */     {
/* 101: 86 */       key[i] = bytesToInt(byteKey, i * 2);
/* 102: 87 */       key[i] = changeBit(key[i]);
/* 103:    */     }
/* 104: 90 */     int i = 1;
/* 105: 91 */     int k = 0;
/* 106: 92 */     for (int j = 8; j < 52; i++)
/* 107:    */     {
/* 108: 93 */       key[(k + i + 7)] = ((key[(k + (i & 0x7))] << 9 | key[(k + (i + 1 & 0x7))] >> 7) & 0xFFFF);
/* 109: 94 */       k += (i & 0x8);
/* 110: 95 */       i &= 0x7;j++;
/* 111:    */     }
/* 112: 98 */     return key;
/* 113:    */   }
/* 114:    */   
/* 115:    */   private int fun_a(int a)
/* 116:    */   {
/* 117:102 */     if (a < 2) {
/* 118:103 */       return a;
/* 119:    */     }
/* 120:105 */     int b = 1;
/* 121:106 */     int c = 65537 / a;
/* 122:107 */     for (int i = 65537 % a; i != 1;)
/* 123:    */     {
/* 124:108 */       int d = a / i;
/* 125:109 */       a %= i;
/* 126:110 */       b = b + c * d & 0xFFFF;
/* 127:111 */       if (a == 1) {
/* 128:112 */         return b;
/* 129:    */       }
/* 130:114 */       d = i / a;
/* 131:115 */       i %= a;
/* 132:116 */       c = c + b * d & 0xFFFF;
/* 133:    */     }
/* 134:118 */     return 1 - c & 0xFFFF;
/* 135:    */   }
/* 136:    */   
/* 137:    */   private int fun_b(int b)
/* 138:    */   {
/* 139:122 */     return 0 - b & 0xFFFF;
/* 140:    */   }
/* 141:    */   
/* 142:    */   private int[] uncrypt_subkey(int[] key)
/* 143:    */   {
/* 144:126 */     int dec = 52;
/* 145:127 */     int asc = 0;
/* 146:128 */     int[] unkey = new int[52];
/* 147:129 */     int aa = fun_a(key[(asc++)]);
/* 148:130 */     int bb = fun_b(key[(asc++)]);
/* 149:131 */     int cc = fun_b(key[(asc++)]);
/* 150:132 */     int dd = fun_a(key[(asc++)]);
/* 151:133 */     unkey[(--dec)] = dd;
/* 152:134 */     unkey[(--dec)] = cc;
/* 153:135 */     unkey[(--dec)] = bb;
/* 154:136 */     unkey[(--dec)] = aa;
/* 155:137 */     for (int k1 = 1; k1 < 8; k1++)
/* 156:    */     {
/* 157:138 */       aa = key[(asc++)];
/* 158:139 */       bb = key[(asc++)];
/* 159:140 */       unkey[(--dec)] = bb;
/* 160:141 */       unkey[(--dec)] = aa;
/* 161:142 */       aa = fun_a(key[(asc++)]);
/* 162:143 */       bb = fun_b(key[(asc++)]);
/* 163:144 */       cc = fun_b(key[(asc++)]);
/* 164:145 */       dd = fun_a(key[(asc++)]);
/* 165:146 */       unkey[(--dec)] = dd;
/* 166:147 */       unkey[(--dec)] = bb;
/* 167:148 */       unkey[(--dec)] = cc;
/* 168:149 */       unkey[(--dec)] = aa;
/* 169:    */     }
/* 170:151 */     aa = key[(asc++)];
/* 171:152 */     bb = key[(asc++)];
/* 172:153 */     unkey[(--dec)] = bb;
/* 173:154 */     unkey[(--dec)] = aa;
/* 174:155 */     aa = fun_a(key[(asc++)]);
/* 175:156 */     bb = fun_b(key[(asc++)]);
/* 176:157 */     cc = fun_b(key[(asc++)]);
/* 177:158 */     dd = fun_a(key[asc]);
/* 178:159 */     unkey[(--dec)] = dd;
/* 179:160 */     unkey[(--dec)] = cc;
/* 180:161 */     unkey[(--dec)] = bb;
/* 181:162 */     unkey[(--dec)] = aa;
/* 182:163 */     return unkey;
/* 183:    */   }
/* 184:    */   
/* 185:    */   private int[] get_subkey(boolean flag, byte[] bytekey)
/* 186:    */   {
/* 187:167 */     if (flag) {
/* 188:168 */       return encrypt_subkey(bytekey);
/* 189:    */     }
/* 190:170 */     return uncrypt_subkey(encrypt_subkey(bytekey));
/* 191:    */   }
/* 192:    */   
/* 193:    */   private byte[] ByteDataFormat(byte[] data, int unit)
/* 194:    */   {
/* 195:175 */     int len = data.length;
/* 196:176 */     int padlen = unit - len % unit;
/* 197:177 */     int newlen = len + padlen;
/* 198:178 */     byte[] newdata = new byte[newlen];
/* 199:179 */     System.arraycopy(data, 0, newdata, 0, len);
/* 200:180 */     for (int i = len; i < newlen; i++) {
/* 201:181 */       newdata[i] = ((byte)padlen);
/* 202:    */     }
/* 203:182 */     return newdata;
/* 204:    */   }
/* 205:    */   
/* 206:    */   public byte[] IdeaEncrypt(byte[] idea_key, byte[] idea_data, boolean flag)
/* 207:    */   {
/* 208:186 */     byte[] format_key = ByteDataFormat(idea_key, 16);
/* 209:    */     
/* 210:188 */     byte[] format_data = ByteDataFormat(idea_data, 8);
/* 211:189 */     int datalen = format_data.length;
/* 212:190 */     int unitcount = datalen / 8;
/* 213:191 */     byte[] result_data = new byte[datalen];
/* 214:192 */     for (int i = 0; i < unitcount; i++)
/* 215:    */     {
/* 216:193 */       byte[] tmpkey = new byte[16];
/* 217:194 */       byte[] tmpdata = new byte[8];
/* 218:195 */       System.arraycopy(format_key, 0, tmpkey, 0, 16);
/* 219:196 */       System.arraycopy(format_data, i * 8, tmpdata, 0, 8);
/* 220:    */       
/* 221:198 */       byte[] tmpresult = Encrypt(tmpkey, tmpdata, flag);
/* 222:199 */       System.arraycopy(tmpresult, 0, result_data, i * 8, 8);
/* 223:    */     }
/* 224:201 */     return result_data;
/* 225:    */   }
/* 226:    */   
/* 227:    */   static int changeBit(int a)
/* 228:    */   {
/* 229:206 */     int javaReadInt = a;
/* 230:    */     
/* 231:208 */     byte byte4 = (byte)(javaReadInt & 0xFF);
/* 232:209 */     byte byte3 = (byte)((javaReadInt & 0xFF00) >> 8);
/* 233:    */     
/* 234:211 */     byte[] newbyte = new byte[2];
/* 235:212 */     newbyte[0] = byte4;
/* 236:213 */     newbyte[1] = byte3;
/* 237:    */     
/* 238:215 */     int ss = newbyte[0];
/* 239:216 */     ss <<= 8;
/* 240:    */     
/* 241:218 */     int ss2 = newbyte[1] & 0xFF;
/* 242:219 */     ss |= ss2;
/* 243:220 */     ss &= 0xFFFF;
/* 244:    */     
/* 245:222 */     return ss;
/* 246:    */   }
/* 247:    */   
/* 248:    */   public static byte[] toNewKey(byte[] mkey)
/* 249:    */   {
/* 250:227 */     char[] zwkey = new char[33];
/* 251:228 */     char[] tkey = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', 
/* 252:229 */       '2', '3', '4', '5', '6' };
/* 253:231 */     for (int i = 0; i < 15; i++)
/* 254:    */     {
/* 255:232 */       int tmp107_106 = i;mkey[tmp107_106] = ((byte)(mkey[tmp107_106] + tkey[i]));
/* 256:    */     }
/* 257:233 */     return mkey;
/* 258:    */   }
/* 259:    */   
/* 260:    */   static byte[] reverseByte(byte[] input)
/* 261:    */   {
/* 262:237 */     for (int i = 0; i < input.length; i++) {
/* 263:238 */       if (i % 2 == 0)
/* 264:    */       {
/* 265:239 */         byte temp = input[i];
/* 266:240 */         input[i] = input[(i + 1)];
/* 267:241 */         input[(i + 1)] = temp;
/* 268:    */       }
/* 269:    */     }
/* 270:244 */     return input;
/* 271:    */   }
/* 272:    */ }


/* Location:           D:\htjs\document\wsrz\WsrzEjbInf\
 * Qualified Name:     gov.sat.tax.fwsk.util.IDEA4Java
 * JD-Core Version:    0.7.0.1
 */